// pages/orderDetail/orderDetail.ts

import {
  orderInfo,
  addressInfo,
  personInfo,
  pauseOrder,
  orderUpdate,
  canttenPhone
} from "../../../http/api"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    orderObj: {
      startDate: '1',
      endDate: '2',
    },

    orderTypeArr: [
      "外卖单",
      "包月单",
      "堂食单",
    ],
    addressInfo: {},
    oldInfo: {},
    personId: "",
    addressId: "",
    phone:""

    //0-未配送 1-已送达 2-已核销 3-暂停配送 4-已退款 5-已顺延
  },

  handleShowOrderCalendar(e){
    const {index} = e.currentTarget.dataset
    const show = this.data.orderObj.orderDetailList[index].show

    this.setData({
      [`orderObj.orderDetailList[${index}].show`]:!show
    })

  },

  handleMakePhone() {

    wx.makePhoneCall({
      phoneNumber: this.data.phone //仅为示例，并非真实的电话号码
    })
  },


  handlePauseOrder(e) {
    const that = this

    const id = e.detail;

    const {
      status
    } = this.data.orderObj


    // 订单状态只能是已支付和进行中的时候才能暂定订单
    if (status != 1 || status != 5) {
      return
    }

    wx.showModal({
      title: '提示',
      content: '确认要暂停该日订单吗？',
      complete: (res) => {

        if (res.confirm) {

          pauseOrder({
            id
          }).then(res => {
            if (res.data.code === 200) {
              wx.showToast({
                title: '提交成功',
              })
              setTimeout(() => {
                that.onShow()
              }, 1000)

            } else {
              wx.showToast({
                title: '操作失败',
                icon: 'error'
              })
            }

          })

        }
      }
    })



  },


  getOldInfo(oldId) {
    wx.showLoading()
    personInfo(oldId).then(oldRes => {
      if (oldRes.data.code === 200) {
        this.setData({
          oldInfo: {
            ...oldRes.data.data
          }
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  getAddressInfo(addressId) {
    wx.showLoading()
    addressInfo(addressId).then(res => {
      if (res.data.code === 200) {
        this.setData({
          addressInfo: {
            ...res.data.data
          }
        })

      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  handleCancleOrder() {
    wx.showModal({
      title: '提示',
      content: '确认取消该订单吗？',
      complete: (res) => {
        if (res.confirm) {

          wx.showLoading()

          const params = {
            id: this.data.orderObj.id,
            status: 4
          }

          orderUpdate(params).then(oldRes => {
            if (oldRes.data.code === 200) {
              wx.showToast({
                title: '订单已取消',
                icon: "success"
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 1000)


            } else {
              wx.showToast({
                title: '操作失败',
                icon: "error"
              })
            }
          }).finally(() => {
            wx.hideLoading()
          })


        }
      }
    })
  },

  handleRefundOrder(){
    wx.showModal({
      title: '提示',
      content: '确认要退款吗？',
      complete: (res) => {
        if (res.confirm) {

          wx.showLoading()

          const params = {
            id: this.data.orderObj.id,
            status: 8
          }

          orderUpdate(params).then(oldRes => {
            if (oldRes.data.code === 200) {
              wx.showToast({
                title: '已提交申请',
                icon: "success"
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 1000)


            } else {
              wx.showToast({
                title: '操作失败',
                icon: "error"
              })
            }
          }).finally(() => {
            wx.hideLoading()
          })


        }
      }
    })

  },

  handlePay() {
    wx.showModal({
      title: '提示',
      content: '即将跳转支付页面...',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {

          const {
            id,
            orderNum,
            amountPrice
          } = this.data.orderObj
          wx.redirectTo({
            url: `/pages/user/pay/pay?id=${id}&orderNum=${orderNum}&amountPrice=${amountPrice}&orderType=0`,
          })

          // wx.redirectTo({
          //   url: '/pages/user/success/success',
          // })
        }
      }
    })
  },

  getData() {

    const {
      id
    } = this.data
    wx.showLoading()
    orderInfo(id).then(res => {
      if (res.data.code === 200) {
        this.setData({
          orderObj: {
            ...res.data.data
          }
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  getShopPhone(){
    wx.showLoading()
    canttenPhone().then(res=>{
      if (res.data.code === 200) {
        this.setData({
          phone:res.data.data.canteenPhone
        })
      }
    }).finally(()=>{
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    if (options.id) {
      this.setData({
        id: options.id
      })
      this.setData({
        orderStatus: app.getOrderStatusArr()
      })
      this.getData()
      this.getShopPhone()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
   
    return

    wx.showModal({
      title: '提示',
      content: '订阅',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          let tmplIds = ['hOVaE5-SSs3mbaJx1pimxuv7MkBY4z_b6pSiRXyPSfA']

          wx.requestSubscribeMessage({
            tmplIds: tmplIds,
            success: (res) => {
              // console.log("订阅消息 成功 " + res);
              wx.showToast({
                title: '订阅消息 成功',
              })
              
            },
            fail: (errCode, errMessage) => {
              wx.showToast({
                title: '订阅消息 错误',
              })
              console.log(errCode, errMessage);
            },
            complete: (errMsg) => {

              console.log("订阅消息 完成 " + errMsg);
            }

          });

        }
      }
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})